package com.zit.dao;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.zit.entity.Classes;
import com.zit.entity.Student;

public class StudentDAO extends BaseDAO {
	
	public String hql(){
		return "select s from Student s left join fetch s.berth b left join fetch b.bedroom br left join fetch br.dorm left join fetch s.classes c left join fetch c.profession p left join fetch c.term t where s.isDelete=0 ";
	}

	/**
	 * 查询所有学生信息
	 * @param student
	 * @param s
	 * @return
	 */
	public List<Student> select(Student student,Session s){
		String hql=hql();
		if(student!=null){
			if(!"".equals(student.getName())){
				hql+=" and s.name like '%'||:name||'%'";
			}
			if(student.getAddr()!=null){
				hql+=" and s.addr like '%'||:addr||'%'";
			}
			if(student.getPhone()!=null){
				hql+=" and s.phone like '%'||:phone||'%'";
			}
			if(student.getGender()!=-1){
				hql+=" and s.gender=:gender";
			}
			if(student.getAge()!=null){
				hql+=" and s.age=:age";
			}
			if(student.getClasses().getId()!=-1){
				hql+=" and s.classes.id=:cid ";
			}
			if(student.getClasses().getTerm().getId()!=-1){
				hql+=" and s.classes.term.id=:tid ";
			}
			if(student.getClasses().getProfession().getId()!=-1){
				hql+=" and s.classes.profession.id=:pid ";
			}
		}
		Query query = s.createQuery(hql);
		if(student!=null){
			if(!"".equals(student.getName())){
				query.setParameter("name", student.getName());
			}
			if(student.getAddr()!=null){
				query.setParameter("addr", student.getAddr());
			}
			if(student.getPhone()!=null){
				query.setParameter("phone", student.getPhone());
			}
			if(student.getGender()!=-1){
				query.setParameter("gender", student.getGender());
			}
			if(student.getAge()!=null){
				query.setParameter("age", student.getAge());
			}
			if(student.getClasses().getId()!=-1){
				query.setParameter("cid", student.getClasses().getId());
			}
			if(student.getClasses().getTerm().getId()!=-1){
				query.setParameter("tid", student.getClasses().getTerm().getId());
			}
			if(student.getClasses().getProfession().getId()!=-1){
				query.setParameter("pid", student.getClasses().getProfession().getId());
			}
		}
		System.out.println(hql);
		return query.list();
	} 
	
	/**
	 * 查询学生信息
	 * @param student
	 * @param s
	 * @return
	 */
	public Student selectAStudent(Student student,Session s){
		String hql=hql()+" and s.id=:sid";
		Query query = s.createQuery(hql);
		query.setParameter("sid", student.getId());
		return (Student)query.uniqueResult();
	} 
	
	/**
	 * 查询对应学期对应专业的班级
	 * @param student
	 * @param s
	 * @return
	 */
	public List<Classes> select(int tid,int pid,Session s){
		Query query = s.createQuery("select new Classes(c.id,c.name) from Classes c where c.term.id=:tid and c.profession.id=:pid").setParameter("tid", tid).setParameter("pid", pid);
		return query.list();
	} 
	
	/**
	 * 添加学生
	 * @param student
	 * @param s
	 */
	public void saveStudent(Student student,Session s){
		s.save(student);
	}
	
	/**
	 * 
	 * @param student
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public void deleteStudent(Student student,Session s) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		delete(student, s);
	}
	
	
}
